<script>
import Person from './components/Person.vue'

export default {
  name: "mainApp",
  components: {
    Person
  }
}
</script>


<template>

  <div class="login-container">
    <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        class="login-form"
    >
      <h2 class="title">系统登录</h2>

      <el-form-item label="用户名" prop="username">
        <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            prefix-icon="Lock"
        />
      </el-form-item>

      <el-form-item>
        <el-button
            type="primary"
            @click="submitForm"
            :loading="loading"
        >
          登录
        </el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import {ref} from 'vue'
import axios from 'axios'

const loginFormRef = ref()
const loading = ref(false)

const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 12, message: '长度在3到12个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur'}
  ]
}

const submitForm = () => {
  loginFormRef.value.validate(valid => {
    if (valid) {
      console.log("触发登录接口！");

      loading.value = true
      // 模拟登录请求
      // setTimeout(() => {
      //   ElMessage.success('登录成功')
      //   loading.value = false
      // }, 1000)


      try {
        //debugger;
        //const response = axios.post('http://localhost:8005/account/login', JSON.stringify());


        axios.post('/api/account/login', loginFormRef.value, {
          headers: {
            'Content-Type': 'application/json'
          }, withCredentials: true
        })
            .then(response =>
                console.log(response)
            )

            .catch(error => console.error(error));

      } catch (error) {

        console.error('Error sending POST request:', error);

      }


    }
  })
}

const resetForm = () => {
  loginFormRef.value.resetFields()
}
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-form {
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  margin-bottom: 30px;
  text-align: center;
  color: #409eff;
}
</style>